package Controller;

import java.awt.event.KeyEvent;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JTextField;

/**
 *
 * @author Esteban Controller to perform the validation of data input througout
 * any User Interface
 */
public class InputController {

    public InputController() {
    }

    public void checkSize(java.awt.event.KeyEvent evt, int maxSize) {
        JTextField obj = (JTextField) evt.getSource();
        if (obj.getText().length() >= maxSize && obj.getSelectionEnd() - obj.getSelectionStart() <= 0) {
            evt.consume();
        }
    }

    public boolean isInteger(String str) {
        boolean a = true;
        try {
            Integer.parseInt(str);
        } catch (NumberFormatException ex) {
            a = false;
        }
        return a;
    }

    public void numbersOnly(java.awt.event.KeyEvent evt) {
        int id = evt.getID();
        if (id == KeyEvent.KEY_TYPED) {
            char c0 = evt.getKeyChar();
            try {
                if (!(Integer.parseInt(String.valueOf(c0)) > 0 || Integer.parseInt(String.valueOf(c0)) < 9)) {
                    evt.consume();
                }
            } catch (NumberFormatException ex) {
                evt.consume();
            }
        }
    }

    public void lettersOnly(java.awt.event.KeyEvent evt) {
        int id = evt.getID();
        if (id == KeyEvent.KEY_TYPED) {
            char c0 = evt.getKeyChar();
            try {
                if ((Integer.parseInt(String.valueOf(c0)) > 0 || Integer.parseInt(String.valueOf(c0)) < 9)) {
                    evt.consume();
                }
            } catch (NumberFormatException ex) {
            }
        }
    }

    public void noSpecialCharacters(java.awt.event.KeyEvent evt) {
        int id = evt.getID();
        if (id == KeyEvent.KEY_TYPED) {
            String REGEX = "[^&%$º'}Çç{·ª#@'?¿´|_''/''\'^'¡¬;:,€~+=)(*!~]*";
            String input = String.valueOf(evt.getKeyChar());
            Pattern pattern = Pattern.compile(REGEX);
            Matcher matcher = pattern.matcher(input);
            if (!matcher.matches()) {
                evt.consume();
            }
        }
    }

    public boolean passwordsMatch(String password, String PasswordConfirmation) {
        return password.equals(PasswordConfirmation);
    }

    public boolean checkFieldSize(String field, int maxLength){
        if(field.length() <= maxLength){
            return true;
        }
        return false;
    }
    
    public void checkAgeBR(java.awt.event.KeyEvent evt) {
        checkSize(evt, 2);
        numbersOnly(evt);
    }

    public void checkCedulaBR(java.awt.event.KeyEvent evt) {
        checkSize(evt, 10);
        numbersOnly(evt);
    }

    public void checkNameBR(java.awt.event.KeyEvent evt) {
        checkSize(evt, 15);
        lettersOnly(evt);
        noSpecialCharacters(evt);
    }

    public void checkLastNameBR(java.awt.event.KeyEvent evt) {
        checkSize(evt, 20);
        lettersOnly(evt);
        noSpecialCharacters(evt);
    }

    public void checkPasswordBR(java.awt.event.KeyEvent evt) {
        checkSize(evt, 16);
        noSpecialCharacters(evt);
    }

    public void checkPhoneBR(java.awt.event.KeyEvent evt) {
        checkSize(evt, 10);
        numbersOnly(evt);
    }

    public void checkProfessionBR(java.awt.event.KeyEvent evt) {
        checkSize(evt, 15);
        lettersOnly(evt);
        noSpecialCharacters(evt);
    }

    public void checkUsernameBR(java.awt.event.KeyEvent evt) {
        checkSize(evt, 14);
        noSpecialCharacters(evt);
    }
}
